<?php
/**
 * Created by JetBrains PhpStorm.
 * File: ArticleAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-14
 * Time: 上午12:32
 */

	class ArticleAction extends CommonAction{

		//指向文章列表
		function index(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
				$article = M('Article');
				import("ORG.Util.page");
				if(isset($_POST['search'])){
					if(empty($_POST['key'])){
						$this->error('不能为空');
					}else{
						$datas = array();
						switch($_POST['search']){
							case 1 :
								$datas['id'] = $_POST['key'];
								break;
							case 2:
								$datas['name'] = array("like","%{$_POST['key']}%");
								break;
						}
						$datas['sectionid'] = $_GET['sid'];
						$datas['categoryid'] = $_GET['cid'];
						$count = $article->where($datas)->count('id');
						if($count<1){
							$this->error('没有该文章数据');
						}elseif($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$list = $article->where($datas)->order('id desc')->select();
						$page = new page($count, C('PAGENUM'));
						$show = $page->fpage($pagedata);
					}
				}else{
					$datas['sectionid'] = $_GET['sid'];
					$datas['categoryid'] = $_GET['cid'];
					$count = $article->where($datas)->count();
					if($count<1){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $article->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->where($datas)->select();
				}
				$this->assign('show', $show);
				$this->assign('list', $list);
				$this->display();
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}

		//指向添加文章页面
		function add(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					$this->display('add');
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}

		}


		//添加文章
		function insert(){
			dump($_POST);
			if((isset($_GET['sid']) && !empty($_GET['sid'])) || (isset($_POST['sectionid']) && !empty($_POST['sectionid']))){
				if((isset($_GET['cid']) && !empty($_GET['cid'])) || (isset($_POST['categoryid']) && !empty($_POST['categoryid']))){
					$article = D('Article');
					if($list = $article->create()){

						if($article->add()){
							if(!empty($_POST)){
								$this->success('添加文章成功', __APP__.'/Article/index/sid/'.$_POST['sectionid'].'/cid/'.$_POST['categoryid']);
							}else{
								$this->success('添加文章成功', __APP__.'/Article/index/sid/'.$_GET['sid'].'/cid/'.$_GET['cid']);
							}
						}else{
							$this->error('添加文章失败');
						}
					}else{
						$this->error($article->getError());
					}
				}else{
					$this->error('异常操作');
				}

			}else{
				$this->error('异常操作');
			}

		}

		function show(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					if(isset($_GET['aid']) && !empty($_GET['aid'])){
						$article = D('Article');

//						$section = D('Section');
//						$da = $section->select();
//						dump($da);
						$datas['sectionid'] = $_GET['sid'];
						$datas['categoryid'] = $_GET['cid'];
						$datas['id'] = $_GET['aid'];
						$list = $article->relation(true)->where($datas)->select();
						$this->assign('list', $list[0]);
						$this->display('show');
					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}

		//删除单元
		function del(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					if(isset($_POST['del']) && !empty($_POST['del'])){
						$article = M('Article');
						foreach($_POST['del'] as $key=>$value){
							$num = $article->delete($value);
						}
						if($num>0){
							$this->success('删除文章成功', __APP__.'/Article/index/sid/'.$_GET['sid'].'/cid/'.$_GET['cid']);
						}else{
							$this->error('删除文章失败');
						}
					}else{
						$this->error('异常操作');
					}

				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}

		//指向修改文章面页
		function edit(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					if(isset($_GET['aid']) && !empty($_GET['aid'])){
						$category = M('Article');
						$list = $category->find($_GET['aid']);
						$this->assign('list', $list);
						$this->display();
					}else{
						$this->error('异常操作');
					}

				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}

		}

		//修改文章
		function update(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['aid']) && !empty($_GET['aid'])){
					if(isset($_GET['cid']) && !empty($_GET['cid'])){
						if(isset($_POST['id']) && !empty($_POST['id'])){
							$section = D('Article');
							if($section->create()){
								if($section->save()){
									$this->success('修改文章成功', __APP__.'/Article/index/sid/'.$_GET['sid'].'/cid/'.$_GET['cid']);
								}else{
									$this->error('修改文章失败');
								}
							}else{
								$this->error($section->getError());
							}
						}else{
							$this->error('异常操作');
						}
					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
	}


		function select(){

			$article = D('Article');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}

					if(isset($_SESSION['mid']) && !empty($_SESSION['mid'])){
						if(isset($_SESSION['cid']) && !empty($_SESSION['cid'])){

							$list = $article->where($datas)->order('id desc')->select();
							$count = $article->where($datas)->count('id');
							if($count<1){
								//	$this->assign('isshow', 'no');
								$this->error('没有该文章数据');
							}elseif($count<C('PAGENUM')){
								$pagedata = array(1);
							}else{
								$pagedata = array(0,1,2,3,4,5,7,8);
							}
							$page = new page($count, C('PAGENUM'));

							$show = $page->fpage($pagedata);
							$this->assign('isshow', 'yes');
							$this->assign('nobody', 'yes');

						}else{
							$this->error('异常操作');
						}
					}else{
						$this->error('异常操作');
					}


				}

			}else{

				if(isset($_GET['mid']) && !empty($_GET['mid'])){
					if(isset($_GET['cid']) && !empty($_GET['cid'])){
						$_SESSION['mid'] = $_GET['mid'];
						$_SESSION['cid'] = $_GET['cid'];
						$count = $article->count();
						if($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$page = new page($count, C('PAGENUM'));
						$show = $page->fpage($pagedata);
						$list = $article->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
						//	dump($list);
						if($count==0){
							$this->assign('isshow', 'no');
							$this->assign('nobody', 'no');
						}else{
							$this->assign('isshow', 'yes');
							$this->assign('nobody', 'yes');
						}
					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}


			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();

		}


		function addall(){
//			$category = M('Category');
//			$lists = $category->field('id,name')->where('sectionid=1')->select();
//			dump($lists);
			$section = M('Section');
			$list = $section->field('id,name')->select();
			$this->assign('list', $list);
			$this->display();
		}

		function returns(){
			$category = M('Category');
			$list = $category->field('id,name')->where('sectionid='.$_POST['id'])->select();
			$data = array();
			foreach($list as $key=>$value){
				$data[] = $value;
			}
			echo json_encode($data);
		}

}